<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Oday 
	{
		public $carID;
		public $carInfo;
		public $date;
		public $odays;
		public $freeOtime;
		public $dayCounter=0;
		private $CI;
		
		public function __construct($inDate)
		{
				$this->CI=& get_instance();
				$this->CI->load->library('form_validation');
				
				if($this->CI->form_validation->valid_date($inDate))
				{
					
				
				
				$this->date=$inDate;
				$this->CI->load->model('OpeningTimes_model');
				$openTimes=$this->CI->OpeningTimes_model->GetRentalTime($this->date);
				
				require_once(APPPATH.'libraries/Otime.php');
				
				foreach($openTimes as $time)
				{
					
					$o1 = new Otime($time['startTime'],$time['endTime']);	
					$this->_AddTime($o1);
					
				}
			
			}
		}
		
		private function _AddTime($otime)
		{
			$this->odays[$this->dayCounter]=$otime;
			$this->dayCounter+=1;
		}
		
		public function ShowDate()
		{
			echo $this->date;
		}
		
		
			public function showIndex()
		{
			echo $this->dayCounter;
		}
		
		
		public function CheckBooked()
		{
			if (!isset($this->odays))
			{
				echo 'det finns inga tider att visa';
			}
			else
			{	
				$this->CI->load->model('Car_model');
				
				foreach($this->odays as $time)
				{
				$result=$this->CI->Car_model->checkIfCarFree($this->carID,$this->date,$time);
				if ($result)
				{
					$time->is_booked('TRUE');
				}
				else
				{
				$time->is_booked('FALSE');
				}
				
				}
			}
		}
		
		public function AddCarInfo()
		{
			if ($this->carID)
			{
				$this->CI->load->model('Car_model');
				$this->carInfo=$this->CI->Car_model->GetCarInfo($this->carID);
			}
		}
		
		private function _generateNextStartTime($endTime)
		{
			$endTimeSplit=explode(':',$endTime);
			return $endTimeSplit[0].':15';
		}
		
		
		public function GenerateFreeOtime()
		{
			
			$tempOtime="";
			$raknare=0;
			

			for ($y=0;$y<=(count($this->odays)-1);$y++)
			{
				if ($this->odays[$y]->booked=='TRUE')
				{
					$tempOtime[$raknare]=$this->odays[$y];
					$raknare+=1;
				}
				
			}

				
			$tempStartTime=NULL;
			$tempEndTime=NULL;
			$i=0;			
			

		if (count($tempOtime)>1)
		{
				for($x=0;$x<=(count($tempOtime)-1);$x++)
				{
						
						if ($tempStartTime==NULL&&$tempEndTime==NULL)
						{
							$tempStartTime=$tempOtime[$x]->startDate;
							$tempEndTime=$tempOtime[$x]->endDate;
						}
						else
						{	
							if($this->_generateNextStartTime($tempEndTime)==$tempOtime[$x]->startDate)
							{
								$tempEndTime=$tempOtime[$x]->endDate;
								if($x==count($tempOtime)-1)
									{
									$this->freeOtime[$i]=array('startTime'=>$tempStartTime,'endTime'=>$tempEndTime);
								}	
							}
							else
							{
								$this->freeOtime[$i]=array('startTime'=>$tempStartTime,'endTime'=>$tempEndTime);
								$i++;
								$tempStartTime=NULL;
								$tempEndTime=NULL;
								
							}
						}
									
				}
			}	
		}
	
		
	}
?>